package com.lhh.springcloud.controller;

import com.lhh.entity.Result;
import com.lhh.springcloud.entity.Order;
import com.lhh.springcloud.service.AccountService;
import com.lhh.springcloud.service.OrderService;
import com.lhh.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 李洪豪
 * @Sno 20220301044
 */
@RestController
@Slf4j
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    @GetMapping("/order/add")
    //Order无@RequestBody注解，请求时需要以表单的方式，否则封装不成功

    public Result addOrder(Order order){
        log.info("添加订单信息-start");
        orderService.addOrder(order);
        log.info("添加订单信息-end");

        log.info("修改库存信息-start");
        storageService.updateStorage(order.getProduct_id(),order.getNums());
        log.info("修改库存信息-end");

        log.info("修改账户信息-start");
        accountService.updateAccountMoney(order.getUser_id(),order.getMoney());
        log.info("修改账户信息-end");

        log.info("修改订单状态-start");
        orderService.updateOrderStatus(order.getUser_id(),1);
        log.info("修改订单状态-end");
        return Result.success("分布式事务操作三表成功",null);
    }
}
